package cn.chair.controller.admin;


import cn.chair.dao.WorkOrderDao;
import cn.chair.entity.WorkOrder;
import cn.chair.model.admin.WorkOrderResp;
import com.cfilmcloud.framework.privilege.api.model.UserProfile;
import life.innovation.builder.SqlBuilder;
import life.innovation.exception.DataReadException;
import life.innovation.exception.DataWriteException;
import life.innovation.model.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 工单处理controller
 */
@RequestMapping(value="work")
@RestController
public class WorkOrderController {

    @Autowired
    @Lazy
    private WorkOrderDao workOrderDao;

    /**
     * 提交订单
     * @param workOrder
     * @param userProfile
     * @return
     * @throws DataWriteException
     */
    @PostMapping(value = "addWorkOrder")
    public Map<String, Object> add(@RequestBody WorkOrder workOrder, UserProfile userProfile) throws DataWriteException {

        workOrder.setUserId(userProfile.getUserId());
        workOrder.setUserAccount(userProfile.getName());
        workOrderDao.insert(workOrder);

        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        return map;
   }

    /**
     * 查询工单列表
     * @param workId
     * @param startDate
     * @param endDate
     * @param description
     * @param userAccount
     * @param page
     * @param countPerPage
     * @param userProfile
     * @return
     */
   @GetMapping(value = "getWorkOrders")
   public WorkOrderResp getWorkOrders(@RequestParam(required = false) Integer workId,
                                      @RequestParam(required = false) String startDate,
                                      @RequestParam(required = false) String endDate,
                                      @RequestParam(required = false) String description,
                                      @RequestParam(required = false) String userAccount,
                                      @RequestParam(required = false, defaultValue = "1") int page,
                                      @RequestParam(required = false, defaultValue = "20") int countPerPage,
                                      UserProfile userProfile) throws DataReadException {

       WorkOrderResp resp = new WorkOrderResp();
        Integer userId = userProfile.getUserId();
       Set<String> set = userProfile.getRoleSet();
       Iterator<String> it = set.iterator();
       while (it.hasNext()){
           String role = it.next();
           if(role.contains("ADMIN")){
               userId = null;
           }
       }
       PageList<WorkOrderResp.WorkOrderList> list = workOrderDao.getWorkOrders(workId,startDate,endDate,description,userAccount,userId,page,countPerPage);
       resp.setPagination(list.getPagination());
       resp.setList(list.getList());
       return resp;

   }

    /**
     * 查看单条工单
     * @param workId
     * @return
     */
   @GetMapping(value="{workId}")
   public WorkOrder getWorkOrder(@PathVariable Integer workId){
       WorkOrder workOrder = workOrderDao.findOne(SqlBuilder.init().where("id",workId));
       return workOrder;
   }

    /**
     * 工单处理
     * @param workId
     * @return
     * @throws DataWriteException
     */
   @PutMapping(value="{workId}")
  public Map<String,Object> update(@PathVariable Integer workId) throws DataWriteException {
     WorkOrder workOrder = workOrderDao.findOne(SqlBuilder.init().where("id",workId));
     if(workOrder != null){
         workOrder.setStatus(WorkOrder.STATUS_DISPOSED);
     }
     workOrderDao.update(workOrder);
     Map<String,Object> map = new HashMap<>();
     map.put("success",true);
     return map;
  }

}
